package com.huang.we_chart.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.we_chart.VO.ProcessVO;
import com.huang.we_chart.VO.RecordVO;
import com.huang.we_chart.VO.RepairVO;
import com.huang.we_chart.VO.Steps;
import com.huang.we_chart.entity.Repair;
import com.huang.we_chart.service.RepairService;
import com.huang.we_chart.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Repair)表控制层
 *
 * @author HuangShen
 * @since 2020-11-29 10:11:35
 */
@RestController
@RequestMapping("repair")
@CrossOrigin
public class RepairController {
    @Resource
    private RepairService repairService;

    @GetMapping("/getAllRecords")
    public Result getAllRecords() {
        List<Repair> repairList = repairService.list();
        ArrayList<RepairVO> repairVOS = new ArrayList<>();

        repairList.forEach(repair -> {
            RepairVO repairVO = new RepairVO();
            repairVO.setDetail(repair.getDetail());
            repairVO.setId(repair.getRepairId());
            repairVO.setImg(repair.getRepairImage());

            repairVO.setState(null == repair.getExamineDate() ? "0" : null == repair.getRepairDate() ? "1" : "2");
            repairVO.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(repair.getCreateDate()));
            repairVOS.add(repairVO);
        });
        return new Result("200", "success", repairVOS);
    }

    @PostMapping("/inserRecord")
    public Result inserRecord(@RequestBody RecordVO recordVO) {
        Repair repair = new Repair();
        Date d = new Date();

        System.out.println(recordVO);
        String[] ss = recordVO.getDate().split("-");
        if(ss[1].length()==1){
            ss[1]="0"+ss[1];
        }
        if (ss[2].length()==1){
            ss[2]="0"+ss[2];
        }

        recordVO.setDate(ss[0]+"-"+ss[1]+"-"+ss[2]);
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

            d= sf.parse(recordVO.getDate()+" "+recordVO.getTime());// 日期转换为时间戳
        } catch (ParseException e) {
            e.printStackTrace();
        }
        repair.setAppointment(d);
        repair.setDetail(recordVO.getDetail());
        repair.setRepairImage(recordVO.getUrl());
        repair.setCreateDate(new Date());
        repair.setLocation(recordVO.getLocation());
        repair.setStudentId(recordVO.getStuid());
        repairService.saveOrUpdate(repair);

        return new Result().ok();

    }

    @GetMapping("/selectRecordById")
    public Result selectRecordById(String id){
        QueryWrapper<Repair> repairQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Repair> repair_id = repairQueryWrapper.eq("repair_id", id);
        Repair one = repairService.getOne(repair_id);
        ProcessVO processVO = new ProcessVO();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        processVO.setCreateDate(simpleDateFormat.format(one.getCreateDate()));
        processVO.setDetail(one.getDetail());
        processVO.setLocation(one.getLocation());
        processVO.setUrl(one.getRepairImage());


        processVO.setExamineDate(null!=one.getExamineDate()?simpleDateFormat.format(one.getExamineDate()):null);
        processVO.setRateDate(null!=one.getRateDate()?simpleDateFormat.format(one.getRateDate()):null );
        processVO.setRepairDate(null!=one.getRepairDate()?simpleDateFormat.format(one.getRepairDate()):null );
        processVO.setRateDate(null!=one.getRateDate()?simpleDateFormat.format(one.getRateDate()):null );

        //steps[steps: [
        //        {
        //          text: '提交申请',
        //          desc: '2020-12-12 12:12',
        //          inactiveIcon: 'arrow-down',
        //          activeIcon: 'star',
        //        },]

        Steps steps0 = new Steps("提交申请",processVO.getCreateDate()==null?"":processVO.getCreateDate(),"arrow-down","star");
        Steps steps1 = new Steps("审核完成",processVO.getExamineDate()==null?"":processVO.getExamineDate(),"arrow-down","star");
        Steps steps2 = new Steps("维修完成",processVO.getRepairDate()==null?"":processVO.getRepairDate(),"arrow-down","star");
        Steps steps3 = new Steps("评价完成",processVO.getRateDate()==null?"":processVO.getRateDate(),"arrow-down","star");
        ArrayList<Steps> objects = new ArrayList<>();
        objects.add(steps0);
        objects.add(steps1);
        objects.add(steps2);
        objects.add(steps3);
        processVO.setSteps(objects);
        System.out.println(processVO.toString());
        return new Result("200","success",processVO);

    }
@GetMapping("/insertPJ")
    public Result insertPJ(String id){
    QueryWrapper<Repair> repair_id = new QueryWrapper<>();
    repair_id.eq("repair_id",id);
    Repair one = repairService.getOne(repair_id);
    one.setRateDate(new Date());
    if (repairService.saveOrUpdate(one))
        return new Result().ok();
    return  new Result().error();




    }


}